package com.shop.test;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.shop.entity.Dept;
import com.shop.util.HibernateUtil;

public class DeptDaoTest2 {
	private Session session;
	
	@Test
	public void testDifferent4(){
		//4.get返回的对象包括完成的数据，load默认只包含id
		 Dept dept1 = session.get(Dept.class,1);
		 System.out.println(dept1);
		 Dept dept2 = session.load(Dept.class,8);
		 System.out.println(dept2.getDname());
	}
	
	
	
	@Test
	public void testDifferent3(){
		//3.数据库不存在对应的记录  get返回null
		Dept dept1 = session.get(Dept.class,1);
		Dept dept11 = session.get(Dept.class,101);
		System.out.println(dept1+"==="+dept11);
		
		Dept dept2 = session.load(Dept.class,1);
		Dept dept22 = session.load(Dept.class,101);
		System.out.println(dept2+"==="+dept22);
		//org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.shop.entity.Dept#101]
		
	}
	
	
	@Test
	public void testDifferent2(){
		//2. 返回值类型 get真实对象的类型， load返回代理对象
		Dept dept1 = session.get(Dept.class,1);
		System.out.println("get=="+dept1.getClass().getName());
		//get==com.shop.entity.Dept
		
		Dept dept2 = session.load(Dept.class,2);
		//load==com.shop.entity.Dept_$$_jvstdaa_0 代理对象
		System.out.println("load=="+dept2.getClass().getName());
	}
	
	@Test
	public void testDifferent(){
		//发送几条SQL语句
		//1. 加载方式  get立即加载 load 延迟加载(使用对象数据才发送SQL)
		Dept dept1 = session.get(Dept.class,1);
		Dept dept2 = session.load(Dept.class,1);
	}
	
	
	
	
	@Before
	public void before(){
		//1.获取session对象
		session  =HibernateUtil.getSesssion();
	}
	@After
	public void after(){
		//5.关闭资源
		HibernateUtil.closeSession(session);
	}
	
	
	
	
	@Test
	public  void testGet() {
		Session session  =HibernateUtil.getSesssion();
	    Dept dept  =session.get(Dept.class, 1);
	    System.out.println(dept);
	    HibernateUtil.closeSession(session);
	}
}
